package com.hemerasolutions.v2trainer.model.training;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.jdo.annotations.Column;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import com.hemerasolutions.v2trainer.model.base.BaseModel;
import com.hemerasolutions.v2trainer.model.library.Library;

@Entity 
public class VersionableTraining extends BaseModel {

	private static final long serialVersionUID = 1L;

    @OneToMany(mappedBy="versionableTraining",cascade = CascadeType.ALL, fetch=FetchType.LAZY)
    private List<Training> trainingVersions;

    @OneToOne
    private Training currentTraining;
    
    @Column
    private Long uuid;

    @ManyToOne
    private Library library;

    
    public VersionableTraining() {
		super();
		this.id = null;
		this.uuid = UUID.randomUUID().getMostSignificantBits();
		this.trainingVersions = new ArrayList<Training>();
	}

	public VersionableTraining(VersionableTraining other, Library library){
		this.library = library;
		
		for(Training training: other.trainingVersions){
			
			Training aux = new Training(training,this);
			
			this.trainingVersions.add(aux);

			if(training.equals(other.currentTraining)){
				this.currentTraining = aux;
			}
		}
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public List<Training> getTrainingVersions() {
		return trainingVersions;
	}

	public void setTrainingVersions(List<Training> trainingVersions) {
		this.trainingVersions = trainingVersions;
	}

	public Training getCurrentTraining() {
		return currentTraining;
	}

	public void setCurrentTraining(Training currentTraining) {
		this.currentTraining = currentTraining;
	}

	public Long getUuid() {
		return uuid;
	}

	public void setUuid(Long uuid) {
		this.uuid = uuid;
	}

	public Library getLibrary() {
		return library;
	}

	public void setLibrary(Library library) {
		this.library = library;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((library == null) ? 0 : library.hashCode());
		result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		VersionableTraining other = (VersionableTraining) obj;
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (library == null) {
			if (other.library != null) {
				return false;
			}
		} else if (!library.equals(other.library)) {
			return false;
		}
		if (uuid == null) {
			if (other.uuid != null) {
				return false;
			}
		} else if (!uuid.equals(other.uuid)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "VersionableTraining [id=" + id + ", uuid=" + uuid
				+ ", library=" + library + "]";
	}
	
	
	    
    
}
